﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasySite.Services;
using NUnit.Framework;

namespace EasySite.Tests.ServiceTests
{
    public class PageCategoryServiceTests : DbUnitTestBase
    {
        [Test]
        public void Should_return_categories()
        {
            var service = base.GetService<IPageCategoryService>();
            var categories = service.GetPageCategories();
            Assert.AreEqual(categories.Count, 2);
            var hasRole = categories.First(c => c.Name == "Dairy").Roles.Contains("Admin");
            Assert.IsTrue(hasRole);
        }

        [Test]
        public void Should_order_by_sequence()
        {
            var service = base.GetService<IPageCategoryService>();
            var categories = service.GetPageCategories();
            if (categories.Count > 1)
            {
                for (var i = 0; i < categories.Count - 1; i++)
                {
                    Assert.IsTrue(categories[i].Sequence < categories[i + 1].Sequence);
                }
            }
            foreach (var category in categories)
            {
                var tempCategory = category;
                while (true)
                {
                    if (tempCategory.Children.Count == 0)
                    {
                        break;
                    }
                    if (tempCategory.Children.Count > 1)
                    {
                        for (var i = 0; i < tempCategory.Children.Count - 1; i++)
                        {
                            Assert.IsTrue(tempCategory.Children[i].Sequence < tempCategory.Children[i + 1].Sequence);
                        }
                    }
                    foreach (var child in tempCategory.Children)
                    {
                        tempCategory = child;
                    }
                }
            }
        }
    }
}
